import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app/models/room_list_item_data.dart';
import 'package:flutter_app/pages/search/search_item.dart';
import 'package:flutter_app/scoped_model/auth.dart';
import 'package:flutter_app/utils/dio_http.dart';
import 'package:flutter_app/utils/scoped_model_helper.dart';
import 'package:flutter_app/widgets/commom_action_button.dart';

class HouseManagement extends StatefulWidget {
  const HouseManagement({Key? key}) : super(key: key);

  @override
  State<HouseManagement> createState() => _HouseManagementState();
}

class _HouseManagementState extends State<HouseManagement> {
  List<RoomListItemData> rentedList = [];

  final List<Tab> myTabs = <Tab>[
    const Tab(text: '已租'),
    const Tab(text: '未租'),
  ];

  _getData() async {
    var auth = ScopedHelperModel.getModel<AuthModel>(context);
    if (!auth.isLogin) return;
    var token = auth.token;
    String url = '/user/houses';
    var res = await DioHttp.of(context).get(url, null, token);
    var resString = json.decode(res.toString());
    // print({resString, 'resString'});
    List dataMap = resString["body"];
    List<RoomListItemData> resList =
        dataMap.map((e) => RoomListItemData.fromJson(e)).toList();
    setState(() {
      rentedList = resList;
    });
  }

  @override
  void initState() {
    Timer.run(_getData); //延迟执行
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length: myTabs.length,
        child: Scaffold(
            floatingActionButtonLocation:
                FloatingActionButtonLocation.centerFloat,
            floatingActionButton: CommomActionButton(
              name: '发布房源',
              onPressed: () {
                Navigator.pushNamed(context, '/releaseHouse');
              },
            ),
            appBar: AppBar(
              title: const Text('房屋管理'),
              bottom: TabBar(tabs: myTabs),
            ),
            body: TabBarView(
              children: [
                ListView(
                  children: rentedList.map((e) => HouseItem(e)).toList(),
                ),
                ListView(
                  children: rentedList.map((e) => HouseItem(e)).toList(),
                )
              ],
            )));
  }
}
